package com.example.androidproject;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

public class RateListActivity extends ListActivity implements Runnable{
    String data[]={"one","two","three"};
    private static final String TAG = "RateListActivity";//写日志的时候要用到
    Handler handler;
    Bundle bundle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_rate_list);
        for(int i = 0;i<data.length;i++){

        }
        ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
        setListAdapter(adapter);
        handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
            }
        };

    }


    public void setHandler(Handler handler) {
        this.handler = handler;
    }
    @Override
    public void run() {
        //获取网络数据，放入list带回到主线程
        List<String> retList=new ArrayList<String>();
        try {
            Thread.sleep(3000);
        }catch (InterruptedException e) {
            e.printStackTrace();
        }
        Log.i(TAG, "run: run()");

        //获取网络数据
        String url=null;
        bundle=new Bundle();
        try {
            url=" https://www.boc.cn/sourcedb/whpj/ ";
            Document doc= Jsoup.connect(url).get();
            //获得页面元素
            Log.i(TAG, "run: title="+doc.title());

            Elements h4s=doc.getElementsByTag("h4");
            for(Element h4:h4s){
                Log.i(TAG, "run: h4="+h4.text());
            }
            //获得表格内容
            Elements tables=doc.getElementsByTag("table");//集合对象
            Element table2=tables.get(1);
            Log.i(TAG, "run: table="+table2);
            Elements trs=table2.getElementsByTag("tr");
            trs.remove(0);
            for(Element tr:trs){
                Elements tds=tr.getElementsByTag("td");
                String cname=tds.get(0).text();
                String cval=tds.get(5).text();
                retList.add(cname+"==>"+cval);
            }
//            url=new URL("https://www.usd-cny.com/bankofchina.htm");
//            HttpURLConnection http=(HttpURLConnection) url.openConnection();
//            InputStream in=http.getInputStream();
//            String html=inputStream2String(in);
//            Log.i(TAG, "run: html="+html);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //发送消息给主线程
        Message msg=handler.obtainMessage();
        msg.what=7;
        //msg.obj="Hello from run";
        msg.obj=retList;
        handler.sendMessage(msg);

    }

}